<?php
/**
 * Created by PhpStorm.
 * User: td
 * Date: 15/1/22
 * Time: 上午1:00
 */

namespace Home\Controller;
use Think\Controller;

class FetcherInfoController extends Controller{

    //乙方个人主页
    public function index(){

        if(isset($_SESSION['loginUser'])){
            $user_id = $_GET['user_id'];
            if(isset($user_id)) {
                if($user_id == $_SESSION['loginUser']['user_id'])
                    $this->assign('curNav', 3);
                else
                    $this->assign('curNav', -1);
                $this->assign('user_id', $user_id);
                $this->assign('isPublish', 0);
                $this->display('Profile/index');
            } else {
//                $this->error('获取用户信息失败');
                $this->display('Profile/index');
            }

        }else{
            $this->display('Login/welcome');
        }
    }

    public function publishProfile(){

        if(isset($_SESSION['loginUser'])){
            $user_id = $_GET['user_id'];
            if(isset($user_id)) {
                if($user_id == $_SESSION['loginUser']['user_id'])
                    $this->assign('curNav', 3);
                else
                    $this->assign('curNav', -1);
                $this->assign('user_id', $user_id);
                $this->assign('isPublish', 1);
                $this->display('Profile/index');
            } else {
//                $this->error('获取用户信息失败');
                $this->display('Profile/index');
            }

        }else{
            $this->display('Login/welcome');
        }
    }
    //pc
    public function indexPC(){
        if(isset($_SESSION['loginUser'])){
            $user_id = $_GET['user_id'];
            if(isset($user_id)) {
                $this->assign('user_id', $user_id);
                $this->theme('PC')->display('Homepage/homepage');
            } else {
                $this->error('获取用户信息失败');
            }

        }else{
            // $this->error('请登陆系统',U('Login/loginPC'));
            $this->theme('PC')->display('Login/login');
        }
    }

    //乙方个人主页基本信息
    public function fetcherInfoBase(){
        if(isset($_SESSION['loginUser'])){
            $user_id = $_GET['user_id'];
            if(IS_GET){
                $user_ext_db = D('UserExt');
                $user_ext_result = $user_ext_db->getFetcherBaseInfo($user_id);
                if($user_ext_result){
                    //dump($user_ext_result);
                    $this->success('获取信息成功',$user_ext_result);
                }else{
                    $this->error('获取信息失败');
                }
            }
        }else{
            $this->error('请登陆系统',U('Login/index'));
        }
    }

    //乙方个人主页信任圈子
    public function trustInfo(){
        if(isset($_SESSION['loginUser'])){
            $trusted_id = $_GET['user_id'];
            if(IS_GET){
                $trust_db = D('Trust');
                $trust_result = $trust_db->getTrusterInfo($trusted_id);
                if($trust_result){
                    $this->success('获取信息成功',$trust_result);
                }else{
                    $this->error('获取信息失败');
                }
            }
        }else{
            $this->error('请登系统',U('Login/index'));
        }
    }

    //乙方个人主页－甲方信息
    public function fetcherJFInfo(){
        if(isset($_SESSION['loginUser'])){
            $user_id = $_GET['user_id'];
            if(IS_GET){
                $user_ext_db = D('UserExt');
                $user_ext_result = $user_ext_db->getFetcherJFInfo($user_id);
                if($user_ext_result){
                    $this->success('获取信息成功',$user_ext_result);
                }else{
                    $this->error('获取信息失败');
                }
            }
        }else{
            $this->error('请登陆系统',U('Login/index'));
        }
    }

    //乙方个人主页－乙方信息
    public function fetcherYFInfo(){
        if(isset($_SESSION['loginUser'])){
            $user_id = $_GET['user_id'];
            if(IS_GET){
                $resume_db = D('Resume');
                $resume_result = $resume_db->getFetcherYFInfo($user_id);
                if($resume_result){
                    $this->success('获取信息成功',$resume_result);
                    // dump($resume_result);
                }else{
                    $this->error('获取信息失败');
                }
            }
        }else{
            $this->error('请登陆系统',U('Login/index'));
        }
    }

    //申请查看乙方简历－发送短信
    public function sendMessageToPhone(){
        if(IS_POST){
            $message = $_POST['message'];
            $user_id = $_POST['user_id'];
            //获取手机号
            $user_db = M('User');
            $user_info = $user_db->getByUser_id($user_id);
            $user_phone = $user_info['phone'];
            //发送消息
            $url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
            $post_data = "account=cf_gwhz&password=XLyxR3&mobile=".$user_phone."&content=".rawurlencode("您的验证码是：".$message."。请不要把验证码泄露给其他人。");
            $result = xml_to_array(post($post_data,$url));
            if($result['SubmitResult']['code'] == 2){
                $this->success('留言已发送');
            }else{
                $this->error('留言发送失败');
            }

        }else{
            $this->error('错误方法');
        }
    }

    //个人设置
    public function settings() {
        $this->assign('curNav', 3);
        if(isset($_SESSION['loginUser'])){
            $this->display('Settings/index');
        }else{
            $this->display('Login/index');
        }
    }

    //个人主页－电话联系
    public function phoneConnect(){
        if(IS_POST){
            $user_id = $_POST['user_id'];
            $user_db = D('User');
            $user_result = $user_db->getPhoneInfo($user_id);
            if($user_result){
                $this->success('获取电话成功',$user_result);
            }else{
                $this->error('获取电话失败');
            }
        }else{
            $this->error('错误方法');
        }
    }

    //对TA公开信息
    public function publicToOne(){
        if(IS_POST){
            $ispublic_one = $_POST['user_id'];
            $user_id = $_SESSION['loginUser']['user_id'];
            $resume_db = D('Resume');
            $resume_result = $resume_db->setPublicToOne($ispublic_one,$user_id);
            if($resume_result){
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }
        }else{
            $this->error('错误方法');
        }
    }

    //个人主页－收藏
    public function collectUser(){
        if(IS_GET){
            $data['collecter_id'] = $_SESSION['loginUser']['user_id'];
            $data['collected_id'] = $_GET['user_id'];
            if( $data['collecter_id'] != $data['collected_id'] ){
                ini_set('date.timezone','Asia/Shanghai');
                $data['collect_time'] = date('Y-m-d H:i:s',time());
                $data['collect_type'] = 1;
                $collect_db = M('Collect');
                $collect_map['collect_type'] = 1;
                $collect_map['collecter_id'] = $data['collecter_id'];
                $collect_map['collected_id'] = $data['collected_id'];
                $collect_exist = $collect_db->where($collect_map)->select();
                if($collect_exist){
                    $collect_result = $collect_db->where($collect_map)->save($data);
                }else{
                    $collect_result = $collect_db->add($data);
                }
                if($collect_result){
                    $this->success('收藏成功');
                }else{
                    $this->error('收藏失败');
                }
            }else{
                $this->error('不能收藏自己');
            }
        }else{
            $this->error('错误方法');
        }
    }

    //个人主页－取消收藏
    public function deleteCollectUser(){
        if(IS_GET){
            $collect_db = M('Collect');
            $collect_map['collect_type'] = 1;
            $collect_map['collecter_id'] = $_SESSION['loginUser']['user_id'];
            $collect_map['collected_id'] = $_GET['user_id'];
            $collect_result = $collect_db->where($collect_map)->delete();
            if($collect_result){
                $this->success('取消收藏成功');
            }else{
                $this->error('取消收藏失败');
            }
        }else{
            $this->error('错误方法');
        }
    }

    //个人主页－留言
    public function leaveMessage(){
        if(IS_POST){
            $data['publisher_id'] = $_SESSION['loginUser']['user_id'];
            $data['receiver_id'] = $_POST['user_id'];
            if( $data['publisher_id'] != $data['receiver_id']){
                $data['content'] = $_POST['content'];
                $data['is_read'] = 1;
                ini_set('date.timezone','Asia/Shanghai');
                $data['time'] = date('Y-m-d H:i:s',time());
                $message_db = M('Message');
                $message_result = $message_db->$message_db->add($data);
                if($message_result){
                    $this->success('留言成功');
                }else{
                    $this->error('留言成功');
                }
            }else{
                $this->error('不能给自己留言');
            }

        }else{
            $this->error('错误方法');
        }
    }

    //个人主页－信任
    public function trustYou(){
        if(IS_GET){
            $trust_db = M('Trust');
            $data['truster_id'] = $_SESSION['loginUser']['user_id'];
            $data['trusted_id'] = $_GET['user_id'];
            if(NULL != $data['truster_id'] && NULL != $data['trusted_id']){
                if( $data['truster_id'] != $data['trusted_id'] ){
                    ini_set('date.timezone','Asia/Shanghai');
                    $data['time'] = date('Y-m-d H:i:s',time());
                    $trust_map['truster_id'] = array('eq',$data['truster_id']);
                    $trust_map['trusted_id'] = array('eq',$data['trusted_id']);
                    $trust_exist = $trust_db->where($trust_map)->select();
                    if(is_array($trust_exist)){
                        $trust_result = $trust_db->where($trust_map)->save($data);
                    }else{
                        $trust_result = $trust_db->add($data);
                    }
                    if($trust_result){
                        $this->success('信任成功');
                    }else{
                        $this->error('信任失败');
                    }
                }else{
                    $this->error('不能信任自己');
                }
            }else{
                $this->error('参数提交错误！');
            }

        }else{
            $this->error('错误方法');
        }
    }

    //个人主页－取消信任
    public function deleteTrustYou(){
        if(IS_GET){
            $trust_db = M('Trust');
            $data['truster_id'] = $_SESSION['loginUser']['user_id'];
            $data['trusted_id'] = $_GET['user_id'];
            $trust_map['truster_id'] = array('eq',$data['truster_id']);
            $trust_map['trusted_id'] = array('eq',$data['trusted_id']);
            $trust_result = $trust_db->where($trust_map)->delete();
            if($trust_result){
                $this->success("操作成功");
            }else{
                $this->error("操作失败");
            }
        }else{
            $this->error('错误方法');
        }
    }

    //个人主页－举报
    public function report(){
        if(IS_POST){
            $report_db = M('Report');
            $data['reporter_id'] = $_SESSION['loginUser']['user_id'];
            $data['reported_id'] = $_POST['user_id'];
            if( $data['reporter_id'] != $data['reported_id'] ){
                $data['content'] = $_POST['content'];
                ini_set('date.timezone','Asia/Shanghai');
                $data['time'] = date('Y-m-d H:i:s',time());
                $data['is_deal'] = 1;
                $report_map['reporter_id'] = array('eq',$data['reporter_id']);
                $report_map['reported_id'] = array('eq',$data['reported_id']);
                $report_exist = $report_db->where($report_map)->select();
                if(is_array($report_exist)){
                    $report_result = $report_db->where($report_map)->save($data);
                }else{
                    $report_result = $report_db->add($data);
                }
                if($report_result){
                    $this->success('举报成功');
                }else{
                    $this->error('举报失败');
                }
            }else{
                $this->error('不能举报自己');
            }

        }else{
            $this->error('错误方法');
        }
    }



    //test
    public function test(){
//        $trusted_id = $_GET['trusted_id'];
//        $trust_db = D('Trust');
//        $trust_result = $trust_db->getTrusterInfo($trusted_id);
//        dump($trust_result);

//        $user_id = $_GET['user_id'];
//        $user_ext_db = D('UserExt');
//        $user_ext_result = $user_ext_db->getFetcherJFInfo($user_id);
//        dump($user_ext_result);
//
        $user_id = $_GET['user_id'];
        $user_ext_db = D('UserExt');
        $user_ext_result = $user_ext_db->getFetcherBaseInfo($user_id);
        dump($user_ext_result);
//
//        $user_id = $_GET['user_id'];
//        $resume_db = D('Resume');
//        $resume_result = $resume_db->getFetcherYFInfo($user_id);
//        dump($resume_result);
    }

} 